<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <el-form
          :model="wareQueryParams"
          ref="wareQueryForm"
          :inline="true"
          v-show="showSearch"
          label-width="58px"
        >
          <el-form-item label="仓库名" prop="wareName">
            <el-input
              v-model="wareQueryParams.wareName"
              placeholder="请输入仓库名"
              clearable
              size="small"
              @keyup.enter.native="handleWareQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="cyan"
              icon="el-icon-search"
              size="mini"
              @click="handleWareQuery"
              >搜索</el-button
            >
          </el-form-item>
          <el-table
            ref="singleTable"
            border
            highlight-current-row
            v-loading="loading"
            :data="wareOptions"
            @row-click="handleWareSelectionChange"
          >
            <el-table-column label="仓库编号" align="center" prop="wareNo" />
            <el-table-column label="仓库名" align="center" prop="wareName" />
          </el-table>
          
        </el-form>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="wareQueryParams.pageNum"
          :limit.sync="wareQueryParams.pageSize"
          @pagination="getWareList"
        />
        <div style="margin-top: 20px">
          <label for="">当前选中的库区:{{ chosenWareNo }}</label>
        </div>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="库位编号" prop="wareLocatorNo">
            <el-input
              v-model="queryParams.wareLocatorNo"
              placeholder="请输入库位编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="库位名称" prop="wareLocatorName">
            <el-input
              v-model="queryParams.wareLocatorName"
              placeholder="请输入库位名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="占用标志" prop="wareLocatorStatus">
            <el-select
              v-model="queryParams.wareLocatorStatus"
              placeholder="请选择占用标志"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in wareLocatorStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="删除标志" prop="deleteFlag">
            <el-select
              v-model="queryParams.deleteFlag"
              placeholder="请选择删除标志"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in deleteFlagOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="cyan"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['systembasic:warelocator:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['systembasic:warelocator:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['systembasic:warelocator:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['systembasic:warelocator:export']"
              >导出</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="warelocatorList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="仓库编号" align="center" prop="wareNo" />
          <el-table-column
            label="库位编号"
            align="center"
            prop="wareLocatorNo"
          />
          <el-table-column
            label="库位名称"
            align="center"
            prop="wareLocatorName"
          />
          <el-table-column label="最大箱数" align="center" prop="maxNumber" />
          <el-table-column
            label="放置物料编号"
            align="center"
            prop="materialNo"
          />
          <el-table-column
            label="占用标志"
            align="center"
            prop="wareLocatorStatus"
            :formatter="wareLocatorStatusFormat"
          />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="创建用户"
            align="center"
            prop="createUserName"
          />
          <el-table-column
            label="修改时间"
            align="center"
            prop="updateTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="修改用户"
            align="center"
            prop="updateUserName"
          />
          <el-table-column
            label="删除标志"
            align="center"
            prop="deleteFlag"
            :formatter="deleteFlagFormat"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['systembasic:warelocator:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['systembasic:warelocator:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改库位管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所在仓库" prop="wareNo">
          <el-select v-model="form.wareNo" placeholder="请选择">
                <el-option
                  v-for="item in wareOptions"
                  :key="item.rowId"
                  :label="item.wareName"
                  :value="item.wareNo"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
        </el-form-item>

        <el-form-item label="库位编号" prop="wareLocatorNo">
          <el-input v-model="form.wareLocatorNo" v-bind:disabled="disableEdit" placeholder="请输入库位编号" />
        </el-form-item>
        <el-form-item label="库位名称" prop="wareLocatorName">
          <el-input
            v-model="form.wareLocatorName"
            placeholder="请输入库位名称"
          />
        </el-form-item>
        <el-form-item label="最大箱数" prop="maxNumber">
          <el-input v-model="form.maxNumber" placeholder="请输入最大箱数" />
        </el-form-item>
        <el-form-item label="放置物料编号" prop="materialNo">
          <el-input
            v-model="form.materialNo"
            placeholder="请输入放置物料编号"
          />
        </el-form-item>
        <el-form-item label="占用标志">
          <el-radio-group v-model="form.wareLocatorStatus">
            <el-radio
              v-for="dict in wareLocatorStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="创建用户" prop="createUserName">
          <el-input
            v-model="form.createUserName"
            placeholder="请输入创建用户"
          />
        </el-form-item>
        <el-form-item label="修改用户" prop="updateUserName">
          <el-input
            v-model="form.updateUserName"
            placeholder="请输入修改用户"
          />
        </el-form-item> -->
        <el-form-item label="删除标志">
          <el-radio-group v-model="form.deleteFlag">
            <el-radio
              v-for="dict in deleteFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWarelocator,
  getWarelocator,
  delWarelocator,
  addWarelocator,
  updateWarelocator,
  exportWarelocator
} from "@/api/systembasic/warelocator";

import { listWare } from "@/api/systembasic/ware";
export default {
  name: "Warelocator",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库名称
      wareName: undefined,
      // 部门树选项
      wareOptions: [],
      chosenWareNo: "",
      // 库位管理表格数据
      warelocatorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 占用标志字典
      wareLocatorStatusOptions: [],
      // 删除标志字典
      deleteFlagOptions: [],
      // 能否编辑编号
      disableEdit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wareNo: null,
        wareLocatorNo: null,
        wareLocatorName: null,
        wareLocatorStatus: null,
        deleteFlag: null
      },
      //仓库查询参数
      wareQueryParams: {
        pageNum: 1,
        pageSize: 30,
        wareNo: null,
        wareName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wareNo: [
          { required: true, message: "仓库编号不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_yes_no").then(response => {
      this.wareLocatorStatusOptions = response.data;
      this.deleteFlagOptions = response.data;
    });
    this.getWareList();
  },

  methods: {
    /** 查询库位管理列表 */
    getList() {
      this.loading = true;
      listWarelocator(this.queryParams).then(response => {
        this.warelocatorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getWareList() {
      this.loading = true;
      listWare(this.wareQueryParams).then(response => {
        this.wareOptions = response.rows;
        this.loading = false;
      });
    },
    handleWareQuery() {
      this.wareQueryParams.pageNum = 1;
      this.getWareList();
       this.chosenWareNo = "";
      this.queryParams.wareNo = this.chosenWareNo;
      this.getList();
    },
    //选择库区
    handleWareSelectionChange(row) {
      this.chosenWareNo = row.wareNo;
      this.queryParams.wareNo = this.chosenWareNo;
      this.getList();
    },
    // 占用标志字典翻译
    wareLocatorStatusFormat(row, column) {
      return this.selectDictLabel(
        this.wareLocatorStatusOptions,
        row.wareLocatorStatus
      );
    },
    // 删除标志字典翻译
    deleteFlagFormat(row, column) {
      return this.selectDictLabel(this.deleteFlagOptions, row.deleteFlag);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rowId: null,
        wareNo: null,
        wareLocatorNo: null,
        wareLocatorName: null,
        maxNumber: null,
        materialNo: null,
        wareLocatorStatus: "N",
        createTime: null,
        createUserName: null,
        updateTime: null,
        updateUserName: null,
        deleteFlag: "N"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wareLocatorNo);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.disableEdit=false;
      this.reset();
      this.open = true;
      this.title = "添加库位管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.disableEdit=true;
      this.reset();
      const wareLocatorNo = row.wareLocatorNo || this.ids;
      getWarelocator(wareLocatorNo).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库位管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rowId != null) {
            updateWarelocator(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarelocator(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const wareLocatorNos = row.wareLocatorNo || this.ids;
      this.$confirm(
        '是否确认删除库位管理编号为"' + wareLocatorNos + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delWarelocator(wareLocatorNos);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有库位管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportWarelocator(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    }
  }
};
</script>
